package gui.cen.read;

import core.rb.r.Pol;
import core.utils.EPara;
import core.utils.Utils;
import gui.Segl2;
import gui.cen.south.HeatMap;
import gui.utils.Borders;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import gui.utils.Para;
import java.util.ArrayList;
import java.util.LinkedList;
import javax.swing.BorderFactory;
import javax.swing.JPanel;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class ReadPane extends JPanel{
    private Graphics2D bg = null;
    private Image offScreen = null;
    private Segl2 SegL2;
    private LinkedList <Color> colors = null;
    public ReadPane(Segl2 SegL2){
        this.SegL2 = SegL2;
        this.setPreferredSize(new Dimension(SegL2.getWidth()-4, SegL2.getHeight() - HeatMap.HEIGHT_OF_HEATMAP - 42));
        this.setBackground(new Color(102, 102, 102));
        if(colors==null){colors = Utils.getColors(Para.PB.getMaxQ());}
        this.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
        this.repaint();
    }

    public void paintComponent(Graphics g) {
        g.setColor(Color.black);
        displayReads(g);
    }

    public void updateReads(){
        this.repaint();
    }

    private void displayReads(Graphics g){
        int noToShow = Para.END_VERTICAL_VIEW - Para.START_VERTICAL_VIEW;
        final int imageWidth = SegL2.getWidth();
        int imageHeightVal = Borders.SEQCHARHEIGHT * noToShow;
        if(imageHeightVal == 0){imageHeightVal = 1;}
        final int imageHeight =  imageHeightVal;
        this.setLayout(new FlowLayout(){public Dimension preferredLayoutSize(Container cont) {return (new Dimension(imageWidth, imageHeight));}});
        offScreen = this.createImage(imageWidth, imageHeight);
        bg = (Graphics2D)offScreen.getGraphics();
        bg.setColor(new Color(102, 102, 102));
        bg.fillRect(0, 0, imageWidth, imageHeight);
        bg.setFont(Borders.getFont(Borders.SEQFONT));
        int displayIndex = 0;
        int t = Para.START_VERTICAL_VIEW;int counter = 0;

        double posteriorThreshold = EPara.POSTFILT/100.00;

        while(counter<noToShow && t<Para.RTD.size()){
            int len = Para.RTD.get(t).getEnd() - Para.RTD.get(t).getStart();
            char [] readChars = new char[len];
            char [] qualChars = new char[len];
            for(int x=0; x<len; x++){
                readChars[x] = '*';
                qualChars[x] = ' ';
            }
            ArrayList <Pol> polys = Para.RTD.get(t).getPols();
            for (int p = 0; p < polys.size(); p++) {
                if (polys.get(p).isIns() == false){
                    if(polys.get(p).isDel() == true){readChars[polys.get(p).getTLoc() - Para.RTD.get(t).getStart()] = '-';}
                    else{
                        if(Para.POSTS!=null){
                            if(polys.get(p).getPost() >= posteriorThreshold){
                                readChars[polys.get(p).getTLoc() - Para.RTD.get(t).getStart()] = polys.get(p).getChar();
                                qualChars[polys.get(p).getTLoc() - Para.RTD.get(t).getStart()] = polys.get(p).getQChar();
                            }
                        }
                        else{
                            readChars[polys.get(p).getTLoc() - Para.RTD.get(t).getStart()] = polys.get(p).getChar();
                            qualChars[polys.get(p).getTLoc() - Para.RTD.get(t).getStart()] = polys.get(p).getQChar();
                        }
                    }
                }
            }
            int startPos = Para.RTD.get(t).getStart();
            int xlocation = (startPos * Borders.SEQ_CHAR_WIDTH) - (Para.TPOS * Borders.SEQ_CHAR_WIDTH);
            int endLoc = (Para.RTD.get(t).getEnd() * Borders.SEQ_CHAR_WIDTH) - (Para.TPOS * Borders.SEQ_CHAR_WIDTH);
            if(endLoc > 0){
                int ylocation = displayIndex * Borders.SEQCHARHEIGHT;
                bg.setColor(new Color(178, 178, 178));
                for(int x=0; x<readChars.length; x++){
                    if(qualChars[x] != ' '){
                        bg.setColor(colors.get((int)qualChars[x]));
                        bg.drawString(readChars[x]+"", xlocation+(x*Borders.SEQ_CHAR_WIDTH), ylocation);
                        bg.setColor(new Color(178, 178, 178));
                    }
                    else{bg.drawString(readChars[x]+"", xlocation + (x*Borders.SEQ_CHAR_WIDTH), ylocation);}
                }
                displayIndex++;
                counter++;
            }
            t++;
        }
        g.drawImage(offScreen, 0, 0, imageWidth, imageHeight, this);
    }  
}